﻿using Microsoft.AspNetCore.SignalR.Client;

namespace OConf.NetCore.SignalR
{
    /// <summary>
    /// SignalR重连策略
    /// 第一次断开立即重连
    /// 第二次断开5秒后重连
    /// 第三次断开15秒后重连
    /// 之后再断开30秒重连一次
    /// </summary>
    internal class RetryPolicy : IRetryPolicy
    {
        public TimeSpan? NextRetryDelay(RetryContext retryContext)
        {
            if (retryContext.PreviousRetryCount <= 1)
            {
                return TimeSpan.Zero;
            }
            else if (retryContext.PreviousRetryCount == 2)
            {
                return TimeSpan.FromSeconds(5);
            }
            else if (retryContext.PreviousRetryCount == 3)
            {
                return TimeSpan.FromSeconds(15);
            }
            else
            {
                return TimeSpan.FromSeconds(30);
            }
        }
    }
}
